package com.ymatou.iapi.dglogin.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.dglogin.parameter.ANewbindBean;
import com.ymatou.iapi.dglogin.parameter.UserWxInfo;
import com.ymatou.iapi.dglogin.parameter.WxLoginSession;
import com.ymatou.iapi.dglogin.service.ANewbindCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.model.DGUserExtInfo;
import com.ymttest.database.model.DGUserInfo;
import com.ymttest.database.model.DGWxUnionidBindInfo;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*登录成功绑定微信
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ANewbind{
	private static ANewbindBean anewbindBean;
	private static ANewbindCall anewbindCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("登录成功绑定微信");
	}

	@Before
	public void caseUp() {
		anewbindBean=new ANewbindBean();
		anewbindCall=new ANewbindCall();
		DGUserWapper=new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(anewbindCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(UserCreateBean usercreateBean2, Integer uid) {
		Logger.comment("验证DGUserInfo");
		DGUserInfo uinfo = DGUserWapper.selectDGUserInfo(uid).get(0);
		Logger.verifyEquals(usercreateBean2.getIscs() == null ? false : usercreateBean2.getIscs(), uinfo.getIsCs(), "验证IsCs");
		//loginid随机生成
		Logger.verifyNotNull(uinfo.getLoginId(), "验证LoginId");
		Logger.verifyNotNull(uinfo.getLoginPwd(), "验证LoginPwd");
		Logger.verifyEquals(usercreateBean2.getLogoUrl(), uinfo.getLogoUrl(), "验证LogoUrl");
		if (usercreateBean2.getMobile() != null) {
			Logger.verifyEquals(usercreateBean2.getMobile(), uinfo.getMobile(), "验证Mobile");
		}
		if (usercreateBean2.getMobileAreaCode() != null) {
			Logger.verifyEquals(usercreateBean2.getMobileAreaCode(), uinfo.getMobileAreaCode(), "验证MobileAreaCode");
		}
		if (usercreateBean2.getBelongSellerId() != null) {
			Logger.verifyEquals(usercreateBean2.getBelongSellerId(), uinfo.getBelongSellerId(), "验证BelongSellerId");
		}
		
		Logger.verifyEquals(usercreateBean2.getNickname(), uinfo.getNickname(), "验证Nickname");
		Logger.verifyEquals(usercreateBean2.getUserGender()==null?0:usercreateBean2.getUserGender(), uinfo.getUserGender(), "验证UserGender");
		Logger.verifyEquals(usercreateBean2.getUserType()==null?0:usercreateBean2.getUserType(), uinfo.getUserType(), "验证UserType");
		Logger.verifyEquals(0, uinfo.getUserStatus(), "验证UserStatus");
		Logger.comment("验证DGUserExtInfo");
		DGUserExtInfo exinfo = DGUserWapper.selectDGUserExtInfo(uid).get(0);
		Logger.verifyEquals(usercreateBean2.getAppSource(), exinfo.getAppSource(), "验证AppSource");
		Logger.verifyEquals(usercreateBean2.getChannelSource()==null?0:usercreateBean2.getChannelSource(), exinfo.getChannelSource(), "验证ChannelSource");
		if (usercreateBean2.getClientIp() != null) {
			Logger.verifyEquals(usercreateBean2.getClientIp(), exinfo.getClientIp(), "验证ClientIp");
		}
		Logger.verifyEquals(usercreateBean2.getCountryCode(), exinfo.getCountryCode(), "验证CountryCode");
		if (usercreateBean2.getInvitationCode() != null) {
			Logger.verifyEquals(usercreateBean2.getInvitationCode(), exinfo.getInvitationCode(), "验证InvitationCode");
		}
		//现在没有remark
//		if (usercreateBean2.getRemark() != null) {
//			Logger.verifyEquals(usercreateBean2.getRemark(), exinfo.getRemark(), "验证Remark");
//		}
		Logger.verifyEquals(usercreateBean2.getTerminalSource()==null?0:usercreateBean2.getTerminalSource(), exinfo.getTerminalSource(), "验证TerminalSource");
		Logger.comment("验证DGThirdPartyBindInfo");
		DGThirdPartyBindInfo tpinfo = DGUserWapper.selectDGThirdPartyBindInfo(uid).get(0);
		// 多绑定关系在login站点考虑 这里主要看数据落地正确
		Logger.verifyEquals(usercreateBean2.getAppSource(), tpinfo.getAppSource(), "验证AppSource");
		Logger.verifyEquals(usercreateBean2.getOpenId(), tpinfo.getOpenId(), "验证OpenId");
		Logger.verifyEquals(usercreateBean2.getChannelSource()==null?0:usercreateBean2.getChannelSource(), tpinfo.getThirdPartyType(), "验证ThirdPartyType");
		if (usercreateBean2.getUnionId() != null) {
			Logger.verifyEquals(usercreateBean2.getUnionId(), tpinfo.getUnionId(), "验证UnionId");
		}
		Logger.verifyEquals(usercreateBean2.getUserType()==null?0:usercreateBean2.getUserType(), tpinfo.getUserType(), "验证UserType");

		Logger.comment("验证DGWxUnionidBindInfo");
		List<DGWxUnionidBindInfo> wxuinfo = DGUserWapper.selectDGWxUnionidBindInfo(uid);
		if (wxuinfo.size() > 0) {
			Logger.verifyEquals(usercreateBean2.getUserType()==null?0:usercreateBean2.getUserType(), wxuinfo.get(0).getUserType(), "验证UserType");
			Logger.verifyEquals(usercreateBean2.getUnionId(), wxuinfo.get(0).getUnionId(), "验证UnionId");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ANewbind_001() {
		Logger.start(true,"用户名密码Seller登录成功绑定微信成功");
		try {
			String oid="oid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			String uid="uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setOpenId(null);
			u.setUnionId(null);
			Integer userid = DGUserCallService.UserCreateCall(u);
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(oid);
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(uid);
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			anewbindBean.setLoginSession(loginSession);
			anewbindBean.setAppSource(u.getAppSource());
			anewbindBean.setAvatarUrl(u.getLogoUrl());
			anewbindBean.setGender(1);
			anewbindBean.setNickname(u.getNickname());
			anewbindBean.setUserId(userid);
			anewbindBean.setWxAppName(u.getWxAppName());
			anewbindCall.setData(anewbindBean);
			anewbindCall.callService();			
			Logger.verifyEquals("true", anewbindCall.getString("success"), "验证success");
			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(wxLoginSession.getOpenId());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			Logger.verifyEquals(uid, list.get(0).getUnionId(), "验证UnionId");
			u.setOpenId(oid);
			u.setUnionId(uid);
			verifyDB(u, userid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ANewbind_002() {
		Logger.start(true,"已绑定商家调用绑定接口失败");
		try {
			String oid="oid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			String uid="uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
//			u.setOpenId(null);
//			u.setUnionId(null);
			Integer userid = DGUserCallService.UserCreateCall(u);
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(oid);
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(uid);
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			anewbindBean.setLoginSession(loginSession);
			anewbindBean.setAppSource(u.getAppSource());
			anewbindBean.setAvatarUrl(u.getLogoUrl());
			anewbindBean.setGender(1);
			anewbindBean.setNickname(u.getNickname());
			anewbindBean.setUserId(userid);
			anewbindBean.setWxAppName(u.getWxAppName());
			anewbindCall.setData(anewbindBean);
			anewbindCall.callService();			
			Logger.verifyEquals("false", anewbindCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ANewbind_003() {
		Logger.start(true,"不输入userid失败");
		try {
			String oid="oid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			String uid="uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setOpenId(null);
			u.setUnionId(null);
			Integer userid = DGUserCallService.UserCreateCall(u);
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(oid);
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(uid);
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			anewbindBean.setLoginSession(loginSession);
			anewbindBean.setAppSource(u.getAppSource());
			anewbindBean.setAvatarUrl(u.getLogoUrl());
			anewbindBean.setGender(1);
			anewbindBean.setNickname(u.getNickname());
//			anewbindBean.setUserId(userid);
			anewbindBean.setWxAppName(u.getWxAppName());
			anewbindCall.setData(anewbindBean);
			anewbindCall.callService();			
			Logger.verifyEquals("false", anewbindCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ANewbind_004() {
		Logger.start(true,"Seller登录成功绑定微信成功-微信信息和注册信息不一致时会更新数据");
		try {
			String oid="oid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			String uid="uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setOpenId(null);
			u.setUnionId(null);
			Integer userid = DGUserCallService.UserCreateCall(u);
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(oid);
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(uid);
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			anewbindBean.setLoginSession(loginSession);
			anewbindBean.setAppSource(u.getAppSource());
			anewbindBean.setAvatarUrl(u.getLogoUrl()+1);
			anewbindBean.setGender(1);
			anewbindBean.setNickname(u.getNickname()+1);
			anewbindBean.setUserId(userid);
			anewbindBean.setWxAppName(u.getWxAppName());
			anewbindCall.setData(anewbindBean);
			anewbindCall.callService();			
			Logger.verifyEquals("true", anewbindCall.getString("success"), "验证success");
			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(wxLoginSession.getOpenId());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			Logger.verifyEquals(uid, list.get(0).getUnionId(), "验证UnionId");
			u.setOpenId(oid);
			u.setUnionId(uid);
			u.setLogoUrl(anewbindBean.getAvatarUrl());
			u.setNickname(anewbindBean.getNickname());
			verifyDB(u, userid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ANewbind_005() {
		Logger.start(true,"已删除用户操作失败");
		try {
			String oid="oid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			String uid="uid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setOpenId(null);
			u.setUnionId(null);
			Integer userid = DGUserCallService.UserCreateCall(u);
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(oid);
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(uid);
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			DGUserCallService.DeleteUser(userid);
			anewbindBean.setLoginSession(loginSession);
			anewbindBean.setAppSource(u.getAppSource());
			anewbindBean.setAvatarUrl(u.getLogoUrl());
			anewbindBean.setGender(1);
			anewbindBean.setNickname(u.getNickname());
			anewbindBean.setUserId(userid);
			anewbindBean.setWxAppName(u.getWxAppName());
			anewbindCall.setData(anewbindBean);
			anewbindCall.callService();			
			Logger.verifyEquals("false", anewbindCall.getString("success"), "验证success");
//			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(wxLoginSession.getOpenId());
//			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
//			Logger.verifyEquals(uid, list.get(0).getUnionId(), "验证UnionId");
//			u.setOpenId(oid);
//			u.setUnionId(uid);
//			verifyDB(u, userid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}